//
//  NSString+Matador.m
//  Matador
//
//  Created by leaf on 15/10/4.
//  Copyright © 2015年 MicroGolds. All rights reserved.
//

#import "NSString+Matador.h"

@implementation NSString (Matador)

+ (BOOL)isEmpty:(NSString *)str {
    if (!str) {
        return YES;
    }
    
    if (![str isKindOfClass:[NSString class]]) {
        return YES;
    }
    
    if ([str isEqualToString:@""]) {
        return YES;
    }
    
    return NO;
}
+ (BOOL)isNotEmpty:(NSString *)str {
    return ![NSString isEmpty:str];
}
+ (NSString *)stringValue:(NSString *)str {
    if (!str) {
        return @"";
    }
    
    if ([str isKindOfClass:[NSNumber class]]) {
        NSNumber *number = (NSNumber *)str;
        return [number stringValue];
    }
    
    if (![str isKindOfClass:[NSString class]]) {
        return @"";
    }
    
    return str;
}
#pragma - 字号设置
- (CGSize)mtdSizeWithFont:(UIFont *)font {
    NSDictionary *attributes = @{NSFontAttributeName:font};
    CGSize size = [self sizeWithAttributes:attributes];
    
    size.width = ceil(size.width);
    size.height = ceil(size.height);
    
    return size;
}
- (CGSize)mtdSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size {
    CGRect rect = [self boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];
    
    rect.size.width = ceil(rect.size.width);
    rect.size.height = ceil(rect.size.height);
    
    return rect.size;
}
- (CGSize)mtdSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = lineBreakMode;
    
    CGRect rect = [self boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle} context:nil];
    
    rect.size.width = ceil(rect.size.width);
    rect.size.height = ceil(rect.size.height);
    
    return rect.size;
}
@end
